package com.example.database.library;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import com.example.book_libapp.CheckInputValue;

import android.util.Log;

public class BookFunctions {

	private JSONParser jsonParser;

	private static String api_key = "nmc-android";

	// private static String BookListURL =
	// "http://10.0.2.2/android_booklib/booklistjsononebook.json";
	// private static String BookListURL =
	// "http://10.0.2.2/android_booklib/latest.json";
	// private static String BookListURL = "http://bookstore-sf14.l:8080/api/" +
	// api_key + "/books/latest.json?page=" + page_number.toString();
	private static String BookListURL = "http://bookstore-sf14.l:8080/api/"
			+ api_key + "/books/latest.json?page=";
	private static String BookInfoURL = "http://bookstore-sf14.l:8080/api/"
			+ api_key + "/books/getInfo/";
	// private static String BookListURL = "http://bookstore-sf14.l:8080/api/" +
	// api_key + "/auth/signin.json";
	private static String BookCommentListURL = "http://bookstore-sf14.l:8080/api/"
			+ api_key + "/books/getReviews/";
	private static String BookDetailSearchURL = "http://bookstore-sf14.l:8080/api/"
			+ api_key + "/books/search.json?query=";
	private static String BookMainSearchURL = "http://bookstore-sf14.l:8080/api/"
			+ api_key + "/books/search.json?query=";
	private static String BookReviewURL = "http://bookstore-sf14.l:8080/api/"
			+ api_key + "/review/";
	
	private static String BookmarkURL = "http://bookstore-sf14.l:8080/api/"
			+ api_key + "/user/";
	// private static String BookCommentListURL =
	// "http://10.0.2.2/android_booklib/comment.json";

	// constructor
	public BookFunctions() {
		// TODO Auto-generated constructor stub
		jsonParser = new JSONParser();
	}

	public JSONObject BookInfo(String book_id) {
		String URL = BookInfoURL + book_id + ".json";
		JSONObject json = jsonParser.getJSONFromUrl(URL);
		return json;
	}

	public JSONObject Booklist(int page_number) {
		String URL = BookListURL + Integer.toString(page_number);
		JSONObject json = jsonParser.getJSONFromUrl(URL);
		return json;
	}

	public JSONObject setRating(String rating) {
		String URL = BookListURL + Integer.toString(1);
		JSONObject json = jsonParser.getJSONFromUrl(URL);
		return json;
	}

	public JSONObject BookReview(int page_number, String bookid,
			String usertoken) {
		String URL = BookCommentListURL + bookid + ".json?page="
				+ Integer.toString(page_number) + "&token=" + usertoken +"";
		Log.e("URL URL", "" + URL);
		JSONObject json = jsonParser.getJSONFromUrl(URL);
		// JSONObject json = jsonParser.getJSONFromUrl(BookCommentListURL);
		return json;
	}

	public JSONObject BookNewReview(String usertoken, String bookid, String review, String rating) {// getReviews/1.json?page=1
		String URL = BookReviewURL + "new.json";
		Log.e("URL URL", "" + URL);

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("token", usertoken));
		params.add(new BasicNameValuePair("book_id", bookid));
		params.add(new BasicNameValuePair("review", review));
		params.add(new BasicNameValuePair("rate", rating));
		JSONObject json = jsonParser.getJSONFromUrl(URL, params);
		return json;
	}
	
	public JSONObject BookDeleteReview(String usertoken, String bookid) {// getReviews/1.json?page=1
		String URL = BookReviewURL + "delete.json";
		Log.e("URL URL", "" + URL);
		
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("token", usertoken));
		params.add(new BasicNameValuePair("book_id", bookid));
		JSONObject json = jsonParser.getJSONFromUrl(URL, params);
		// JSONObject json = jsonParser.getJSONFromUrl(BookCommentListURL);
		return json;
	}
	
	public JSONObject BookEditReview(String usertoken, String bookid, String review, String rating) {// getReviews/1.json?page=1
		String URL = BookReviewURL + "edit.json";
		Log.e("URL URL", "" + URL + " " + usertoken + " " + bookid);
		
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("token", usertoken));
		params.add(new BasicNameValuePair("book_id", bookid));
		params.add(new BasicNameValuePair("review", review));
		params.add(new BasicNameValuePair("rate", rating));
		JSONObject json = jsonParser.getJSONFromUrl(URL, params);
		
		// JSONObject json = jsonParser.getJSONFromUrl(BookCommentListURL);
		return json;
	}
	
	public JSONObject getBookmark(String token) {
		String URL = BookmarkURL + "getBookmark.json?token=" + token + "";
		JSONObject json = jsonParser.getJSONFromUrl(URL);
		return json;
	}
	
	public JSONObject addBookmark(String token, String book_id) {
		String URL = BookmarkURL + "addBookmark.json?token=" + token + "&book_id=" + book_id +"";
		JSONObject json = jsonParser.getJSONFromUrl(URL);
		return json;
	}
	
	public JSONObject deleteBookmark(String token, String book_id) {
		String URL = BookmarkURL + "deleteBookmark.json?token=" + token + "&book_id=" + book_id +"";
		JSONObject json = jsonParser.getJSONFromUrl(URL);
		return json;
	}
	
	public JSONObject getHistory(String token, String page, String date_from, String date_to) {
		String URL = BookmarkURL + "getHistory.json?token=" + token;
		CheckInputValue checkvalue = new CheckInputValue();
		if (checkvalue.urlparams(page)) {
			URL += "&page=" + page;
		} 
		if (checkvalue.urlparams(date_from)) {
			URL += "&date_from=" + date_from;
		}
		
		if (checkvalue.urlparams(date_to)) {
			URL += "&date_to=" + date_to;
		}
		JSONObject json = jsonParser.getJSONFromUrl(URL);
		return json;
	}
	
	public JSONObject BookDetailSearch(String key, String catg, String sort,
			String sort_type, String bestseller, String page, String count) {
		String URL = BookDetailSearchURL + key; 
		CheckInputValue checkvalue = new CheckInputValue();
		if (checkvalue.urlparams(catg)) {
			URL += "&catg=" + catg;
		} 
		if (checkvalue.urlparams(sort)) {
			URL += "&sort=" + sort;
		}
		if (checkvalue.urlparams(sort_type)) {
			URL += "&sort_type=" + sort_type;
		}
		if (checkvalue.urlparams(bestseller)) {
			URL += "&bestseller=" + bestseller;
		}
		if (checkvalue.urlparams(page)) {
			URL += "&page=" + page;
		}
		if (checkvalue.urlparams(count)) {
			URL += "&count=" + count;
		}
		
		JSONObject json = jsonParser.getJSONFromUrl(URL);
		return json;
	}
}
